package com.pair.micro.common.sign;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.pair.micro.common.constant.MicroBusinessError;
import com.pair.micro.common.constant.MicroSystemError;
import com.pair.micro.common.exception.MicroBusinessException;
import com.pair.micro.common.exception.MicroSystemException;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 签名工具
 *
 * @author menglt
 * @since 12/29/20 3:28 PM
 */
@Slf4j
public class MicroSignUtil {

  /**
   * @name: buildSign
   * @description 创建签名
   * @author menglt
   * @date 12/29/20 3:45 PM
   * @param
   * @return: java.lang.String
   */
  public static String buildSign(MicroSign microSign) {
    StringBuilder sb = new StringBuilder();
    sb.append("(");
    sb.append(microSign.getJyKey());
    sb.append(MicroSignConstants.SIGN_SEPARATOR);
    sb.append(microSign.getNonceStr());
    sb.append(MicroSignConstants.SIGN_SEPARATOR);
    sb.append(microSign.getTimestampStr());
    sb.append(")");
    sb.append(microSign.getSignSalt());

    return SecureUtil.md5(sb.toString()).toUpperCase();
  }

  /**
   * @name: checkSign
   * @description 验证签名（以服务器时间为准）
   * @author menglt
   * @date 12/29/20 3:45 PM
   * @param
   * @return: boolean
   */
  public static boolean checkSign(MicroSign microSign) {

    if (StrUtil.isBlank(microSign.getTimestampStr())) {
      throw new MicroBusinessException(MicroBusinessError.BIS_10001, "summer时间戳为空");
    }

    // 入参时间戳
    long reqtimestamp = Long.parseLong(microSign.getTimestampStr());

    // 系统时间戳
    long nowtimestamp = DateUtil.current();

    // 时间差 单位：秒
    long difftimestamp = (nowtimestamp - reqtimestamp) / 1000;

    // 延迟时间
    Integer delayTimes = microSign.getDelayTimes();
    if (delayTimes > 0) {
      delayTimes = -delayTimes;
    }

    // 时间差
    Integer diffTimes = microSign.getDiffTimes();

    if (difftimestamp < delayTimes || difftimestamp > diffTimes) {
      throw new MicroSystemException(MicroSystemError.SYS_00301);
    }

    String jySign = buildSign(microSign);

    if (!jySign.equals(microSign.getJySign())) {
      throw new MicroSystemException(MicroSystemError.SYS_00301);
    }

    return true;
  }

  /**
   * @name: buildSignExample
   * @description 构建签名示例
   * @author menglt
   * @date 1/5/21 11:25 AM
   * @param
   * @return: void
   */
  private void buildSignExample() {

    /** 锁客链授权key */
    String jyKey = "1232486248594149376";
    /** 锁客链随机字符串 */
    String nonceStr = "d307ffe2876247b89ef93de5f7e3a17e";
    /** 锁客链时间戳 */
    Date nowDate = new Date();
    String timestampStr = StrUtil.toString(nowDate.getTime());
    /** 签名盐 */
    String signSalt = "Jycloud2020";

    StringBuilder sb = new StringBuilder();
    sb.append("(");
    sb.append(jyKey);
    sb.append("&");
    sb.append(nonceStr);
    sb.append("&");
    sb.append(timestampStr);
    sb.append(")");
    sb.append(signSalt);

    String jySign = SecureUtil.md5(sb.toString()).toUpperCase();
    System.out.println("锁客链签名：" + jySign);
  }

  public static void main(String[] args) {
    // 当前时间
    //    Date nowDate = new Date();
    //    MicroSign jycloudSign = new MicroSign();
    //    jycloudSign.setJyKey("1232486248594149376");
    //    jycloudSign.setNonceStr("d307ffe2876247b89ef93de5f7e3a17e");
    //    jycloudSign.setTimestampStr(StrUtil.toString(nowDate.getTime()));
    //    System.out.println(jycloudSign);
    //    String sign = MicroSignUtil.buildSign(jycloudSign);
    //    jycloudSign.setJySign("buildSign：" + sign);
    //    System.out.println(sign);
    //    System.out.println("checkSign：" + MicroSignUtil.checkSign(jycloudSign));

    StringBuilder developer_sign = new StringBuilder();
    developer_sign.append("menglt");
    developer_sign.append("lijx");
    developer_sign.append("wangzs");
    developer_sign.append(MicroSignConstants.SIGN_SALT);
    System.out.println(SecureUtil.md5(developer_sign.toString()).toUpperCase());
  }
}
